﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PointAsLine
{
    public class Segment
    {
        public PointF2G Start { get; set; }
        public PointF2G End { get; set; }

        public string Name { get; set; }

        public Segment(PointF2G start, PointF2G end)
        {
            Start = start;
            End = end;
        }

        public bool IsPointOnSegment(PointF2G p)
        {
            var cross = (End.X - Start.X) * (p.Y - Start.Y) - (End.Y - Start.Y) * (p.X - Start.X);
            if (Math.Abs(cross) > 1e-2) return false;

            var minX = Math.Min(Start.X, End.X);
            var maxX = Math.Max(Start.X, End.X);
            var minY = Math.Min(Start.Y, End.Y);
            var maxY = Math.Max(Start.Y, End.Y);

            return p.X >= minX - 1e-4 && p.X <= maxX + 1e-4 &&
                   p.Y >= minY - 1e-4 && p.Y <= maxY + 1e-4;
        }
        public override string ToString()
        {
            return Start.ToString() + " " + End.ToString() + " " + Name;
        }
    }
}
